<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Rubik Ultimate Framework
 * @version 	: 1.1
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 * 
 * Created on	:  Jun 27, 2008
 * 
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );

RCoreImporter::cls( "access.UserGroupList" );

/**
 * Class for holding the factory of UserAccess
 * 
 * @static 
 */
class UserAccessFactory {
	
	/**
	 * Get the GroupList of the system
	 *
	 * @return UserGroupList
	 */
	function &getGroupList() {
		static $groupList;
		if (empty ( $groupList )) {
			$groupList = new UserGroupList ( );
			$anyGroup = new UserGroup ( "Any Body", GROUP_EVERY_ONE );
			$groupList->addGroup ( $anyGroup );
		}
		return $groupList;
	}
	
	/**
	 * Get the UserGroup that is stored in the system
	 *
	 * @param string $code group code
	 * @return UserGroup
	 */
	function &getUserGroup($groupCode) {
		$groupList = UserAccessFactory::getGroupList ();
		return $groupList->getGroup ( $groupCode );
	}
	
	/**
	 * Add UserGroup
	 *
	 * @param UserGroup $userGroup
	 */
	function addUserGroup($userGroup) {
		$groupList = &UserAccessFactory::getGroupList ();
		$groupList->addGroup ( $userGroup );
	}
	
	/**
	 * Check an current groupCode can access to the system resource?
	 *
	 * @param string $groupCode
	 * @return boolean
	 */
	function checkSystemResourceAccess($groupCode) {
		$group = UserAccessFactory::getUserGroup ( $groupCode );
		$sysResource = SysRequest::getResource ();
		if ($group != null) {
			if ($group->canAccess ( $sysResource )) {
				return true;
			}
		}
		$group = UserAccessFactory::getUserGroup ( GROUP_EVERY_ONE );
		return $group->canAccess ( $sysResource );
	}
	
	/**
	 * Add the access right for a groupCode or array of GroupCode to access a resource
	 *
	 * @param SiteResource $resource
	 * @param string $groupCode
	 * @param string ... more group code
	 */
	function addAccessResource($resource, $groupCode) {
		$args = func_get_args ();
		$size = count ( $args );
		
		for($i = 1; $i < $size; $i ++) {
			$group = $args [$i];
			$userGroup = UserAccessFactory::getUserGroup ( $group );
			if ($userGroup) {
				$userGroup->addAccessResource ( $resource );
			}
		}
	}
	
	/**
	 * Add the denied access right for a groupCode or array 
	 * of GroupCode to access a resource
	 *
	 * @param SiteResource $resource
	 * @param string $groupCode
	 * @param string ... more group code
	 */
	function addDineResource($resource, $groupCode) {
		$args = func_get_args ();
		$size = count ( $args );
		
		for($i = 1; $i < $size; $i ++) {
			$group = $args [$i];
			$userGroup = UserAccessFactory::getUserGroup ( $group );
			if ($userGroup) {
				$userGroup->addDineResource ( $resource );
			}
		}
	}
	
	/**
	 * Check if the user of system can access
	 *
	 * @return boolean
	 */
	function checkSystemUserAccess() {
		$groupCode = UserAccessFactory::getSystemUserGroup ();
		return UserAccessFactory::checkSystemResourceAccess ( $groupCode );
	}
	
	/**
	 * Set the system user Group.
	 * This function must be call one time after init the user
	 *
	 * @param string $groupCode
	 */
	function setSysUserGroup($groupCode) {
		$sysGroup = & UserAccessFactory::getSystemUserGroup ();
		$sysGroup = $groupCode;
	}
	
	/**
	 * Get the system user group code
	 *
	 * @return string
	 */
	function &getSystemUserGroup() {
		static $groupCode;
		return $groupCode;
	}
	
	/**
	 * Add the access right for a groupCode or array of GroupCode to access a resource
	 *
	 * @param strig $accessKey
	 * @param string $groupCode
	 * @param string ... more group code
	 */
	function addAccessKey($accessKey, $groupCode) {
		$args = func_get_args ();
		$size = count ( $args );
		$resource = &UserAccessFactory::_getAccessKeyResource($accessKey);
		
		for($i = 1; $i < $size; $i ++) {
			$group = $args [$i];
			$userGroup = UserAccessFactory::getUserGroup ( $group );
			if ($userGroup) {
				$userGroup->addAccessResource ( $resource );
			}
		}
	}
	
	/**
	 * Check if user group can access to a key
	 *
	 * @param string $accessKey
	 * @param string $groupCode if group code = null --> get current user group code
	 */
	function checkAccessKey($accessKey, $groupCode = null) {
		if (empty($groupCode)) 
		{
			$groupCode = UserAccessFactory::getSystemUserGroup();
		}
		$group = UserAccessFactory::getUserGroup ( $groupCode );
		$res = &UserAccessFactory::_getAccessKeyResource($accessKey);
		if ($group != null) {
			if ($group->canAccess ( $res )) {
				return true;
			}
		}
		$group = UserAccessFactory::getUserGroup ( GROUP_EVERY_ONE );
		return $group->canAccess ( $res );
	}
	
	/**
	 * Function get the resource of access key
	 *
	 * @param string $accessKey
	 * @return SiteResource
	 */
	function &_getAccessKeyResource($accessKey) {
		return new SiteResource('__access_key_', $accessKey);
	}
}
?>
